home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap15 / ShowDib1 / DibFile.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.5 KB  |  122 lines

  1. /*---------------------------------
  2.    DIBFILE.C -- DIB File Functions
  3.   ---------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <commdlg.h>
  7. #include "dibfile.h"
  8.  
  9. static OPENFILENAME ofn ;
  10.  
  11. void DibFileInitialize (HWND hwnd)
  12. {
  13.      static TCHAR szFilter[] = TEXT ("Bitmap Files (*.BMP)\0*.bmp\0")  \
  14.                                TEXT ("All Files (*.*)\0*.*\0\0") ;
  15.      
  16.      ofn.lStructSize       = sizeof (OPENFILENAME) ;
  17.      ofn.hwndOwner         = hwnd ;
  18.      ofn.hInstance         = NULL ;
  19.      ofn.lpstrFilter       = szFilter ;
  20.      ofn.lpstrCustomFilter = NULL ;
  21.      ofn.nMaxCustFilter    = 0 ;
  22.      ofn.nFilterIndex      = 0 ;
  23.      ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
  24.      ofn.nMaxFile          = MAX_PATH ;
  25.      ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
  26.      ofn.nMaxFileTitle     = MAX_PATH ;
  27.      ofn.lpstrInitialDir   = NULL ;
  28.      ofn.lpstrTitle        = NULL ;
  29.      ofn.Flags             = 0 ;             // Set in Open and Close functions
  30.      ofn.nFileOffset       = 0 ;
  31.      ofn.nFileExtension    = 0 ;
  32.      ofn.lpstrDefExt       = TEXT ("bmp") ;
  33.      ofn.lCustData         = 0 ;
  34.      ofn.lpfnHook          = NULL ;
  35.      ofn.lpTemplateName    = NULL ;
  36. }
  37.  
  38. BOOL DibFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  39. {
  40.      ofn.hwndOwner         = hwnd ;
  41.      ofn.lpstrFile         = pstrFileName ;
  42.      ofn.lpstrFileTitle    = pstrTitleName ;
  43.      ofn.Flags             = 0 ;
  44.      
  45.      return GetOpenFileName (&ofn) ;
  46. }
  47.  
  48. BOOL DibFileSaveDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
  49. {
  50.      ofn.hwndOwner         = hwnd ;
  51.      ofn.lpstrFile         = pstrFileName ;
  52.      ofn.lpstrFileTitle    = pstrTitleName ;
  53.      ofn.Flags             = OFN_OVERWRITEPROMPT ;
  54.      
  55.      return GetSaveFileName (&ofn) ;
  56. }
  57.  
  58. BITMAPFILEHEADER * DibLoadImage (PTSTR pstrFileName)
  59. {
  60.      BOOL               bSuccess ;
  61.      DWORD              dwFileSize, dwHighSize, dwBytesRead ;
  62.      HANDLE             hFile ;
  63.      BITMAPFILEHEADER * pbmfh ;
  64.  
  65.      hFile = CreateFile (pstrFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
  66.                          OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;
  67.  
  68.      if (hFile == INVALID_HANDLE_VALUE)
  69.           return NULL ;
  70.  
  71.      dwFileSize = GetFileSize (hFile, &dwHighSize) ;
  72.  
  73.      if (dwHighSize)
  74.      {
  75.           CloseHandle (hFile) ;
  76.           return NULL ;
  77.      }
  78.  
  79.      pbmfh = malloc (dwFileSize) ;
  80.  
  81.      if (!pbmfh)
  82.      {
  83.           CloseHandle (hFile) ;
  84.           return NULL ;
  85.      }
  86.  
  87.      bSuccess = ReadFile (hFile, pbmfh, dwFileSize, &dwBytesRead, NULL) ;
  88.      CloseHandle (hFile) ;
  89.  
  90.      if (!bSuccess || (dwBytesRead != dwFileSize)         
  91.                    || (pbmfh->bfType != * (WORD *) "BM") 
  92.                    || (pbmfh->bfSize != dwFileSize))
  93.      {
  94.           free (pbmfh) ;
  95.           return NULL ;
  96.      }
  97.      return pbmfh ;
  98. }
  99.  
  100. BOOL DibSaveImage (PTSTR pstrFileName, BITMAPFILEHEADER * pbmfh)
  101. {
  102.      BOOL   bSuccess ;
  103.      DWORD  dwBytesWritten ;
  104.      HANDLE hFile ;
  105.  
  106.      hFile = CreateFile (pstrFileName, GENERIC_WRITE, 0, NULL,
  107.                          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
  108.  
  109.      if (hFile == INVALID_HANDLE_VALUE)
  110.           return FALSE ;
  111.  
  112.      bSuccess = WriteFile (hFile, pbmfh, pbmfh->bfSize, &dwBytesWritten, NULL) ;
  113.      CloseHandle (hFile) ;
  114.  
  115.      if (!bSuccess || (dwBytesWritten != pbmfh->bfSize))
  116.      {
  117.           DeleteFile (pstrFileName) ;
  118.           return FALSE ;
  119.      }
  120.      return TRUE ;
  121. }
  122.